package com.zpark.wxshop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpark.wxshop.Utils.Result;

import com.zpark.wxshop.service.IReviewService;
import com.zpark.wxshop.entity.Review;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PJone
 * @since 2023-09-04
 */
@RestController
@RequestMapping("/review")
public class ReviewController {

    @Resource
    private IReviewService reviewService;

    /**
    * 新增与修改
    */
    @PostMapping
    public Result save(@RequestBody Review review) {
        reviewService.saveOrUpdate(review);
        return Result.success();
    }

    /**
    * 根据id删除
    */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        reviewService.removeById(id);
        return Result.success();
    }

    /**
    * 批量删除
    */
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        reviewService.removeByIds(ids);
        return Result.success();
    }

    /**
    * 查询所有
    */
    @GetMapping
    public Result findAll() {
        return Result.success(reviewService.list());
    }

    /**
    * 根据id查询单个
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(reviewService.getById(id));
    }

    /**
    * 分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(reviewService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

